package com.zt.bigdata.helloclient.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.bigdata.entity.User;
import com.zt.bigdata.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: zhangtong
 * @Date: 2018/9/13 上午9:19
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public Integer add(User user) {
        user.setDelFlag(0);
        return userMapper.insert(user);
    }

    @Override
    public Integer delete(Long id) {
        User user = userMapper.selectById(id);
        if (user == null || user.getDelFlag() == 1) {
            return null;
        }
        user.setDelFlag(1);
        return update(user);
    }

    @Override
    public Integer update(User user) {
        User result = userMapper.selectById(user.getId());
        if (result == null) {
            return null;
        }
        return userMapper.updateById(user);
    }

    @Override
    public IPage<User> list(Integer sex, Integer pageIndex, Integer pageSize) {
        Page<User> page = new Page(pageIndex - 1, pageSize);
        Wrapper wrapper = new QueryWrapper<User>().eq("sex", sex);
        return userMapper.selectPage(page, wrapper);
    }
}
